﻿-- Author  : Kurapica
-- Create Date : 2009/12/27

do
	IGAS:NewAddon("IGAS_Studio")
	
	local _Addon
	
	local _NewScriptFile = [[
IGAS:NewAddon("@Addon")

]]
	
	local _NewLocalFile = [[
local L = IGAS:NewLocal("@Addon", "@Localization", @Default)
if not L then return end

]]
	
	local _NewFrameFile = [[
IGAS:NewAddon("@Addon.Mdl_@Name")

]]

	local _NewWidgetFile = [[
IGAS:NewAddon("@Addon")

local _WidgetName = "@Name"
local _Base = "@Base"

-- ScriptType
local _ScriptType = {
}

-- Function
local _Function = {
}

-- Property
local _Property = {
}

-- New      
local _New = function(parent)
	local frame = CreateFrame(_Base, nil, parent)
	
	return frame
end

local _WidgetInfo = {
	["WidgetName"] = _WidgetName,
	["Base"] = _Base,
	["Function"] = _Function,
	["Property"] = _Property,
	["ScriptType"] = _ScriptType,
	["New"] = _New,
}

@Name = IGAS:NewWidget(_WidgetInfo)
]]

	-- NewFileInfo Form
	local frm = CreateFrame("Form", "NewFileInfo", coreF)
	frm.Visible = false
	frm.Caption = L["New File"]
	frm.BorderStyle = "TOOLTIP"
	frm.TitleBarAlign = "LEFT"
	frm.Width = 250
	frm.Height = 150
	frm.Resizable = false

	local lblType = CreateFrame("FontString", "lblType", frm, "OVERLAY","GameFontHighlight")
	lblType.Text = L["File Type"]
	lblType:SetPoint("TOPLEFT", frm, "TOPLEFT", 4, -30)

	local cboType = CreateFrame("ComboBox", "FileType", frm)
	cboType:SetPoint("TOPRIGHT", frm, "TOPRIGHT", -4, -30)
	cboType.Width = 150
	cboType.Editable = false
	cboType:SetList{
		["Localization"] = "Localization",
		["Script"] = "Script",
		["Widget"] = "Widget",
		["Frame"] = "Frame",
	}
	cboType.Value = "Script"

	local lblName = CreateFrame("FontString", "lblName", frm, "OVERLAY", "GameFontHighlight")
	lblName.Text = L["File Name"]
	lblName:SetPoint("TOP", lblType, "BOTTOM", 0, -16)

	local cboName = CreateFrame("ComboBox", "inpName", frm)
	cboName:SetPoint("TOP", cboType, "BOTTOM", 0, -4)
	cboName.Width = 150
	cboName.HideDropDownButton = true
	cboName.Editable = true
	cboName:SetList{
		["enUS"] = "enUS",
		["frFR"] = "frFR",
		["deDE"] = "deDE",
		["koKR"] = "koKR",
		["zhCN"] = "zhCN",
		["zhTW"] = "zhTW",
		["ruRU"] = "ruRU",
		["esES"] = "esES",
		["esMX"] = "esMX",
	}
	
	local cboWidgetBase = CreateFrame("ComboBox", "WidgetBase", frm)
	cboWidgetBase:SetPoint("TOPLEFT", cboName, "BOTTOMLEFT", 0, -4)
	cboWidgetBase.Width = 150
	cboWidgetBase.Visible = false
	for i in pairs(IGAS.GUI.Widget) do
		cboWidgetBase:AddItem(i, i)
	end

	local btnOkay = CreateFrame("NormalButton", "Okay", frm)
	btnOkay.Text = L["Okay"]
	btnOkay.Style = "NORMAL"
	btnOkay:SetPoint("BOTTOMLEFT", frm, "BOTTOMLEFT", 4, 24)
	btnOkay.Width = 80
	btnOkay.Height = 24

	local btnReset = CreateFrame("NormalButton", "Rest", frm)
	btnReset.Text = L["Reset"]
	btnReset.Style = "NORMAL"
	btnReset:SetPoint("LEFT", btnOkay, "RIGHT")
	btnReset.Width = 80
	btnReset.Height = 24

	local btnCancel = CreateFrame("NormalButton", "Cancel", frm)
	btnCancel.Text = L["Cancel"]
	btnCancel.Style = "NORMAL"
	btnCancel:SetPoint("LEFT", btnReset, "RIGHT")
	btnCancel.Width = 80
	btnCancel.Height = 24

	------------------------------------------------------
	-- Scripts
	------------------------------------------------------
	local substr = {}
	
	local function Decode(var)
		return (substr[var] and substr[var]) or ""
	end
	
	local function GetContent(script)
		return gsub(script, "@(%a+)", Decode)
	end
	
	function cboType:OnValueChanged(value)
		if value == "Localization" then
			cboWidgetBase.Visible = false
			cboName.HideDropDownButton = false
			cboName.Editable = false
			cboName.Text = "enUS"
		elseif value == "Widget" then
			cboWidgetBase.Visible = true
			cboName.HideDropDownButton = true
			cboName.Editable = true
			cboName.Text = ""
			cboWidgetBase.Text = "Frame"
		else
			cboWidgetBase.Visible = false
			cboName.HideDropDownButton = true
			cboName.Editable = true
			cboName.Text = ""
		end
	end
	
	function cboName:OnEnterPressed()
		btnOkay:Fire("OnClick")
	end
	
	function btnOkay:OnClick()
		if strtrim(cboName.Text) == "" then
			IGAS:MsgBox(L["Please input the file name."])
			return
		end
		local folder
		
		for _, f in ipairs(IGAS_STUDIO_DB.FileDB[_Addon]) do
			if type(f) == "table" and f.Type == "Folder" and f.Name == cboType.Value then
				folder = f
				break
			end
		end
		
		if folder then
			for i, v in ipairs(folder) do
				if type(v) == "table" and v.Type ~= "Folder" and v.Name == cboName.Text then
					IGAS:MsgBox(L["There is a file with that name."])
					return
				end
			end
		end
		
		frm:Hide()
		
		wipe(substr)
		
		substr["Addon"] = _Addon
		substr["Name"] = cboName.Text
		
		if cboType.Value == "Localization" then
			substr["Localization"] = cboName.Text
			substr["Default"] = tostring(cboName.Text == "enUS")
			tinsert(folder,  { ["Type"] = "Localization", ["Name"] = cboName.Text, ["Content"] = GetContent(_NewLocalFile), })
		elseif cboType.Value == "Script" then
			tinsert(folder,  { ["Type"] = "Script", ["Name"] = cboName.Text, ["Content"] = GetContent(_NewScriptFile), })
		elseif cboType.Value == "Widget" then
			substr["Base"] = cboWidgetBase.Text
			tinsert(folder,  { ["Type"] = "Widget", ["Name"] = cboName.Text, ["Content"] = GetContent(_NewWidgetFile), })
		elseif cboType.Value == "Frame" then
			tinsert(folder,  { ["Type"] = "Frame", ["Name"] = cboName.Text, ["Designer"] = GetContent(_NewFrameFile), ["Script"] = GetContent(_NewFrameFile),})
		end
		
		frm:FireEvent("IGAS_STUDIO_NEWFILE_SAVE", _Addon, cboType.Value, cboName.Text)
	end
	
	function btnReset:OnClick()
		cboType.Value = "Script"
		cboName.Text = ""
	end
	
	function btnCancel:OnClick()
		frm:Hide()
	end
	
	------------------------------------------------
	-- Custom Events
	------------------------------------------------	
	frm:RegisterEvent("IGAS_STUDIO_NEWFILE_REQUIRE")
	function frm:IGAS_STUDIO_NEWFILE_REQUIRE(addon)
		if addon and IGAS_STUDIO_DB.FileDB[addon] then
			_Addon = addon
			cboType.Value = "Script"
			cboName.Text = ""
			frm:Show()
		end
	end
end